package cz.boxdesign.library.entity.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cz.boxdesign.library.common.dao.DatabaseOperationException;
import cz.boxdesign.library.entity.Napsal;
import cz.boxdesign.library.entity.dao.query.NapsalSqlQuery;

@Component("napsalDao")
public class NapsalDaoImpl extends EntityDaoAbstract<Napsal, NapsalSqlQuery> {

  @Autowired
  @Qualifier("autorDao")
  private AutorDaoImpl autorDao;

  @Autowired
  @Qualifier("knihaDao")
  private BookDaoImpl knihaDao;

  @Override
  void getEntityList(List<Napsal> result, ResultSet rs) throws SQLException, DatabaseOperationException {
    while (rs.next()) {
      Napsal n = new Napsal();
      n.setAutor(autorDao.getById(rs.getInt("AUTOR_ID_A")));
      n.setKniha(knihaDao.getById(rs.getInt("KNIHA_ID_K")));
      result.add(n);
    }
  }

  @Override
  String getSqlQuery(int queryValue) {
    return NapsalSqlQuery.getByValue(queryValue).getQuery();
  }

  @Override
  void fillStatement(Napsal entity, int queryValue, PreparedStatement ps) throws SQLException {
    NapsalSqlQuery clause = NapsalSqlQuery.getByValue(queryValue);
    switch (clause) {
      case UPDATE: {
        ps.setInt(3, entity.getKniha().getId());
      }
      case INSERT: {

      }
      case DELETE: {
        ps.setInt(1, entity.getAutor().getId());
        ps.setInt(2, entity.getKniha().getId());
        break;
      }
      default:
        throw new IllegalArgumentException();
    }
  }

}
